import { produce } from 'immer';
import lodash from 'lodash';
import { NewWorkflowFile, EventItemType } from './dict';

const Model = {
  namespace: 'wfeditor',

  state: {
    appName: null,
    wfName: null,
    wfid: null,
    // 从服务器请求过来的原始数据
    wfContent: {
      processDefinitionKey: '', // 流程名
      categoryId: '', // 流程分类id
      bindFormType: 1,
      defaultRouterName: '/test1',
      bpmnJson: null, // 节点数据
      entryId: '', // 流程id
      entryPublishId: '', // 流程版本
    },
    formList: [],

    // 图上的 工作节点，审批节点，开始节点，边 等需要编辑属性对应的 Model 列表
    // 供右侧的属性面板使用
    // 1. 在图数据请求得到之后，分析得到初始 graphCells，之后图上的 新增、删除、编辑，均需要同步过来
    // 2. 在保存的时候，需要将额外定制的属性合并到图列表里面去
    graphCells: NewWorkflowFile(),
    // graphCells: [],
    propList: [],
    changeStatus: false, // 触发自动保存

    selectedId: null,
    selectedType: null,
    selectedObj: null,
  },

  effects: {
    *addCellEffect({ payload }, { call, put, select }) {},

    *deleteCellEffect({ payload }, { call, put, select }) {},

    *updateCellEffect({ payload }, { call, put, select }) {},
    // *addCellEffect({ payload }, { call, put, select }) {
    //   let propList = yield select((state) => state.wfeditor.propList);
    //   let changeStatus = yield select((state) => state.wfeditor.changeStatus);
    //   let itemData = {
    //     desc: '',
    //     nodeDesc: '',
    //     operateData: [],
    //     memberList: [],
    //     sendList: [],
    //     pagePath: '',
    //     formKey: {
    //       projectId: '',
    //       pageId: '',
    //       pageNameCh: '',
    //       pageNameEn: '',
    //       pagePath: '',
    //       pageType: '',
    //     },
    //   };
    //   if (payload.type == EventItemType.WorkTask) {
    //     itemData.desc = '流程节点';
    //     itemData.memberList = [];
    //   } else if (payload.type == EventItemType.VerifyTask) {
    //     itemData.desc = '审批节点';
    //     itemData.memberList = [];
    //     itemData.approvalType = 'orSign';
    //   } else if (payload.type == EventItemType.Edge) {
    //     itemData.desc = '连线';
    //     itemData.editType = 'edit';
    //     itemData.meetType = 'one';
    //     itemData.conditionList = [];
    //     itemData.equation = {
    //       componentColumns: [],
    //       conditionExpression: '',
    //     };
    //   }
    //   propList.push({
    //     id: payload.id,
    //     data: itemData,
    //   });
    //   yield put({ type: 'setPropList', payload: propList });
    //   yield put({ type: 'setStatus', payload: !changeStatus });
    //   console.log('addCellEffect', propList);
    // },
    //
    // *deleteCellEffect({ payload }, { call, put, select }) {
    //   let propList = yield select((state) => state.wfeditor.propList);
    //   let changeStatus = yield select((state) => state.wfeditor.changeStatus);
    //   propList.forEach((item, index) => {
    //     if (item.id == payload.id) {
    //       propList.splice(index, 1);
    //     }
    //   });
    //   yield put({ type: 'setPropList', payload: propList });
    //   yield put({ type: 'setStatus', payload: !changeStatus });
    //   console.log('deleteCellEffect', payload);
    // },
    //
    // *updateCellEffect({ payload }, { call, put, select }) {
    //   let propList = yield select((state) => state.wfeditor.propList);
    //   let changeStatus = yield select((state) => state.wfeditor.changeStatus);
    //   if (payload.value == 'multiSign') {
    //     propList.forEach((item) => {
    //       if (item.id == payload.id) {
    //         item.data[payload.prop] = payload.value;
    //         item.data.memberList = [];
    //         item.data.sendList = [];
    //       }
    //     });
    //   } else {
    //     propList.forEach((item) => {
    //       if (item.id == payload.id) {
    //         item.data[payload.prop] = payload.value;
    //       }
    //     });
    //   }
    //   yield put({ type: 'setPropList', payload: propList });
    //   yield put({ type: 'setStatus', payload: !changeStatus });
    //   // console.log('updateCellEffect', payload);
    // },
  },

  reducers: {
    // 通用的修改整个根级对象的方法，自行通过 key 覆盖值
    setRootObj(state, { payload }) {
      return { ...state, ...payload };
    },

    selected(state, { payload: { id: selectedId, type: selectedType } }) {
      return { ...state, selectedId, selectedType };
    },

    addCell(state, { payload: { id, type } }) {
      console.log(`add cell id: ${id}, ${type}`);
      return { ...state };
    },

    deleteCell(state, { payload: { id } }) {
      console.log(`delete id: ${id}`);
      return { ...state };
    },

    updateCell(state, { payload }) {
      return { ...state };
    },

    // 更新画布节点
    updateGraphCells(state, { payload }) {
      return { ...state, graphCells: payload };
    },

    // 当前流程对象
    setSelectedFlow(state, { payload }) {
      return { ...state, wfContent: payload };
    },

    // 表单列表
    setFormList(state, { payload }) {
      return { ...state, formList: payload };
    },

    // 所有节点下的属性配置
    setPropList(state, { payload }) {
      return { ...state, propList: payload };
    },

    // 需要更新一次
    setStatus(state, { payload }) {
      return { ...state, changeStatus: payload };
    },
  },
};

export default Model;
